package com.subsequent.controller;

import com.subsequent.entity.DepartmentPharmacyConfig;
import com.subsequent.service.DepartmentPharmacyConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/departmentPharmacyConfig")
public class DepartmentPharmacyConfigController {

    @Autowired
    private DepartmentPharmacyConfigService service;

    // 列表查询
    @GetMapping("/list")
    public Map<String, Object> list(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String level,
            @RequestParam(required = false) String pharmacy,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer parentId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        List<DepartmentPharmacyConfig> data = service.list(name, level, pharmacy, status, parentId, startDate, endDate, page, pageSize);
        int total = service.count(name, level, pharmacy, status, parentId, startDate, endDate);
        Map<String, Object> result = new HashMap<>();
        result.put("data", data);
        result.put("total", total);
        return result;
    }

    // 新增
    @PostMapping("/add")
    public void add(@RequestBody DepartmentPharmacyConfig config) {
        service.add(config);
    }

    // 编辑
    @PostMapping("/edit")
    public void edit(@RequestBody DepartmentPharmacyConfig config) {
        service.update(config);
    }

    // 删除
    @PostMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        service.delete(id);
    }

    // 查询单条
    @GetMapping("/{id}")
    public DepartmentPharmacyConfig getById(@PathVariable Integer id) {
        return service.getById(id);
    }
}